﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Aricie.DNN.Services.Url.FriendlyUrl;
using System.Web.Behaviors;
using DotNetNuke.Entities.Tabs;
using System.Web.Moles;
using Aricie.DNN.Test.TestFramework.Services.Mocks;
using Aricie.DNN.Services.Url.FriendlyUrl.Moles;

namespace Aricie.FriendlierUrlProvider.Test.Components.Entities.Groups
{
    [TestClass]
    public class FriendlierUrlRuleTestFromFormerStrategy
    {

        private  BHttpContext _bContext;




        [TestMethod]
        [HostType("Moles")]
        public void TestFromFormerStrategyWithAliases()
        {
            FriendlierUrlStrategy strategy = new FriendlierUrlStrategy();
            FriendlierUrlSettings settings = new FriendlierUrlSettings();

            strategy.RewriteRule = "${BASE_REPLACERULE_ROOT}${BASE_REPLACERULE_END}";

            FriendlierUrlRule result = FromFormerStrategy(strategy, settings);

            Assert.AreNotEqual(result, null);
        }

        [TestMethod]
        [HostType("Moles")]
        public void TestFromFormerStrategyWithSimpleGroups()
        {
            MFriendlierUrlContext fupContext = new MFriendlierUrlContext(FriendlierUrlContext.Instance);
            MFriendlierUrlContext.InstanceGet = () => fupContext;
            FriendlierUrlSettings settings = new FriendlierUrlSettings();

            fupContext.FriendlierUrlPortalSettingsGet = () => settings;
            fupContext.NoCacheGet = () => true;

            FriendlierUrlStrategy strategy = new FriendlierUrlStrategy();

            strategy.RewriteRule = "[${Scheme}][${UserInfo}][${RootPath}][/${Lang}][/${TabPath,SubPath}][/${TabName}][/${ModuleTitle}][/${ControlKey}][/${Params,SubPath}][.${Extension}][?${QueryString,QueryStringValue}][#${Fragment,QueryStringFlag}]";
            strategy.StrategyProviders.Add(new UrlProviderInfo() { Name = "StandardUrlRewriterProvider", ProviderType = "Aricie.DNN.Services.Url.FriendlyUrl.StandardUrlRewriterProvider, Aricie.FriendlierUrlProvider" });

            FriendlierUrlRule result = FromFormerStrategy(strategy, settings);

            Assert.AreNotEqual(result, null);
        }

        [TestMethod]
        [HostType("Moles")]
        public void TestFromFormerStrategyTabPath()
        {
            MFriendlierUrlContext fupContext = new MFriendlierUrlContext(FriendlierUrlContext.Instance);
            MFriendlierUrlContext.InstanceGet = () => fupContext;
            FriendlierUrlSettings settings = new FriendlierUrlSettings();

            fupContext.FriendlierUrlPortalSettingsGet = () => settings;
            fupContext.NoCacheGet = () => true;

            FriendlierUrlStrategy strategy = new FriendlierUrlStrategy();

            strategy.RewriteRule = "[/${TabPath,SubPath,%[a-z]?%}-]";
            strategy.StrategyProviders.Add(new UrlProviderInfo() { Name = "StandardUrlRewriterProvider", ProviderType = "Aricie.DNN.Services.Url.FriendlyUrl.StandardUrlRewriterProvider, Aricie.FriendlierUrlProvider" });

            FriendlierUrlRule result = FromFormerStrategy(strategy, settings);

            Assert.AreNotEqual(result, null);
            Assert.AreEqual(result.Instances.Count, 1);
            Assert.IsInstanceOfType(result.Instances[0], typeof(ProviderUrlGroup));

            ProviderUrlGroup tabPathGroup = (ProviderUrlGroup)result.Instances[0];

            Assert.AreEqual(tabPathGroup.RewriteType, RewriteType.SubPath);
            Assert.AreEqual(tabPathGroup.Prefix, "/");
            Assert.AreEqual(tabPathGroup.Regex, "[a-z]?");
            Assert.AreEqual(tabPathGroup.Suffix, "-");

        }

        private FriendlierUrlRule FromFormerStrategy(FriendlierUrlStrategy strategy, FriendlierUrlSettings settings)
        {
            HttpContextMocks.Mock();
            SettingsControllerMocks.Mock();
            DotNetNukeMocks.Mock();

            return FriendlierUrlRule.FromFormerStrategy(strategy, settings);

        }

    }
}
